package com.bridgeintelligent.tag.customer.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.bridgeintelligent.tag.customer.ess.EssConfig;
import com.bridgeintelligent.tag.customer.ess.EssQueryModel;
import com.bridgeintelligent.tag.customer.ess.EssResult;
import com.bridgeintelligent.tag.customer.ess.IEssApproveService;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.HttpUtil;
import com.bridgeintelligent.tag.utils.SecurityHelper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 探查分析设置服务层接口实现
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
@Slf4j
public class EssApproveServiceImpl implements IEssApproveService {

    private final EssConfig config;

    @Override
    public EssResult essAuth(String yuaId) {

        try {
            EssQueryModel queryModel = EssQueryModel.of().setYuaId(yuaId).setClientId(config.getClientId());
            String jsonStr = JSONUtil.toJsonStr(queryModel);
            log.info("员工自助-认证请求url:{},请求参数：{}",config.getAuthUrl(),jsonStr);

            String response = HttpUtil.doPostJson(config.getAuthUrl(), jsonStr,"");
            log.info("员工自助-认证响应结果：{}",response);
            if (StrUtil.isNotBlank(response)) {
                return JSONUtil.toBean(response,EssResult.class);
            }else{
                return EssResult.of().setCode(-1).setMsg("员工自助-请求响应无结果！");
            }
        }catch (Exception e){
            log.error("员工自助-认证请求异常",e);
            return EssResult.of().setCode(-1).setMsg("员工自助-认证请求异常"+e.getMessage());
        }

    }

    @Override
    public EssResult essUser(String yuaId) {
        try {
            User user = SecurityHelper.currentUser();
            if (!Objects.equals(yuaId,user.getYuSid())){
                return EssResult.of().setCode(-1).setMsg("请求信息与登录信息不匹配！");
            }
            EssResult essResult1 = this.essAuth(yuaId);
            if (essResult1.getCode() != 0){
                return essResult1;
            }
            EssQueryModel queryModel = EssQueryModel.of().setYuaId(yuaId).setYyzzToken(essResult1.getYyzzToken());
            String jsonStr = JSONUtil.toJsonStr(queryModel);
            log.info("员工自助-人员列表请求url:{},请求参数：{}",config.getUserUrl(),jsonStr);

            String response = HttpUtil.doPostJson(config.getUserUrl(), jsonStr,queryModel.getYyzzToken());
            log.info("员工自助-人员列表响应结果：{}",response);
            if (StrUtil.isNotBlank(response)) {
                return JSONUtil.toBean(response,EssResult.class);
            }else{
                return EssResult.of().setCode(-1).setMsg("员工自助-人员列表请求响应无结果！");
            }
        }catch (Exception e){
            log.error("员工自助-人员列表请求异常",e);
            return EssResult.of().setCode(-1).setMsg("员工自助-人员列表请求异常"+e.getMessage());
        }
    }

    @Override
    public EssResult essProcess(EssQueryModel queryModel) {
        try {
            EssResult essResult1 = this.essAuth(queryModel.getYuaId());
            if (essResult1.getCode() != 0){
                return essResult1;
            }
            queryModel.setYyzzToken(essResult1.getYyzzToken());

            String jsonStr = JSONUtil.toJsonStr(queryModel);
            log.info("员工自助-发起流程请求url:{},请求参数：{}",config.getProcessUrl(),jsonStr);

            String response = HttpUtil.doPostJson(config.getProcessUrl(), jsonStr,queryModel.getYyzzToken());
            log.info("员工自助-发起流响应结果：{}",response);
            if (StrUtil.isNotBlank(response)) {
                return JSONUtil.toBean(response,EssResult.class);
            }else{
                return EssResult.of().setCode(-1).setMsg("员工自助-发起流请求响应无结果！");
            }
        }catch (Exception e){
            log.error("员工自助-发起流请求异常",e);
            return EssResult.of().setCode(-1).setMsg("员工自助-发起流请求异常"+e.getMessage());
        }
    }
}
